package com.yc.jpyy.mobile.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.mobile.dao.BizDao;
import com.yc.jpyy.mobile.service.BizService;

@Controller
@RequestMapping(value = "/class")
public class GetClassInformation extends BaseController{
	@Autowired
	private BizService bizService;
	@Autowired
	private BizDao bizDao;
	private static Logger logger = LoggerFactory.getLogger(GetClassInformation.class);
	
	/**
	 * 根据驾校ID或者教练ID获取班型列表
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getClassInfo", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getClassInfo(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 驾校Id
			String driverId = request.getParameter("driverId");
			// 驾校类型
			String driveType = request.getParameter("driveType");
			//教练ID
			String teacherId = request.getParameter("teacherId");
			
			if((driverId != null && !driverId.isEmpty())
					&& (driveType != null && !driveType.isEmpty())){
				Map<String, Object> hashMap = new HashMap<String, Object>();
				hashMap.put("driverId", driverId);
				hashMap.put("driveType", driveType);
				// 获取该驾校下的所有班型
			    response = bizService.getClassInfoByDriverId(hashMap,response);
			}else if(teacherId != null && !teacherId.isEmpty()){
				// 通过教练所在的总驾校，然后获取该总驾校下的所有班型
				Map<String, Object> hashMap = bizService.getDriverByTeacher(teacherId);
				if(hashMap != null && hashMap.size()>0){
				    response = bizService.getClassInfoByDriverId(hashMap,response);
				}else{
					response.setErrorcode(1);
					response.setMessage("班型信息为空...");
				}
			}
		} catch (Exception e) {
			logger.info("getClassInfo:"+ e.toString());
			response.setReturnMsg(0,"获取班型信息失败！");
		} finally {
			return response.toJSON();
		}

	}

	
	/**
	 * 根据班型ID获取班型详情
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getClassDetailById", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getClassDetailById(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 班型Id
			String classId = request.getParameter("classId");
			
		    if(classId != null && !classId.isEmpty()){
		    	// 获取数据集合
			    Map<String, Object> listmap = bizDao.getClassDetailById(classId);
				if (listmap != null && listmap.size() > 0) {
					response.setErrorcode(0);
					response.setData(listmap);
				} else {
					response.setErrorcode(1);
					response.setMessage("该班型信息为空...");
				}
		    }else{
		    	response.setErrorcode(1);
				response.setMessage("班型Id不能为空");
		    }
			

		} catch (Exception e) {
			logger.info("getClassDetailById:"+ e.toString());
			response.setReturnMsg(0,"获取班型信息失败！");
		} finally {
			return response.toJSON();
		}

	}

}
